package com.mkyong;

import java.util.Date;

import org.hibernate.Session;

import com.mkyong.stock.Stock;
import com.mkyong.stock.StockDailyRecord;
import com.mkyong.util.HibernateUtil;

public class AppOneToMany {
	
	public static void main(String[] args) {
		
		System.out.println("Hibernate one to many (Annotation");
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		session.beginTransaction();
		
		Stock stock = new Stock();
		stock.setStockCode("7053");
		stock.setStockName("PANINI");
		session.save(stock);
		
		StockDailyRecord stockDailyRecords = new StockDailyRecord();
		stockDailyRecords.setPriceOpen(new Float(1.1));
		stockDailyRecords.setPriceClose(new Float(10.0));
		stockDailyRecords.setPriceChange(new Float(1.2));
		stockDailyRecords.setVolume(3000000L);
		stockDailyRecords.setDate(new Date());
		
		stockDailyRecords.setStock(stock);
		stock.getStockDailyRecord().add(stockDailyRecords);
		
		session.save(stockDailyRecords);
		
		session.getTransaction().commit();
		System.out.println("Done!");
	}

}
